package com.songVersion.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.songVersion.model.Song;
import com.songVersion.services.NestManager;
import com.songVersion.utils.PMF;

public class NestManagerImpl extends ManagerImpl<Song> implements NestManager
{

	/*
	 * no lo usamos ya que utilizamos el objeto NestCache
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Song> searchByTitleAndAuthor(String title, String author)
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			Query query = pm.newQuery(Song.class);
			query.setFilter("author == authorParam && title == tittleParam");
			query.declareParameters("String authorParam, String tittleParam");
			List<Song> l = (List<Song>) query.execute(author, title);
			List<Song> resSongs = cloneList(l);
			return resSongs;
		} finally
		{
			pm.close();
		}
	}

	private List<Song> cloneList(List<Song> l)
	{
		List<Song> res = new ArrayList<Song>();
		for (Song s:l)
		{
			Song newSong = new Song(s); //se colna para evitar error con pm cerrado
			
			l.add(newSong);
		}
		return res;
	}

	@Override
	public void init()
	{
		this.t = new Song("", "");
		
	}
	

}
